/*
* $Id: JournalTreeView.cpp 70 2010-03-20 20:21:36Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#pragma warning(disable:4312)
#include "Common/include.h"
#include "Components/Journal/Styles.h"
#include "Database/Operation/RenameOperation.h"
#include "Database/Operation/DeleteOperation.h"
#include "JournalTreeView.h"
#include "ViewJournal.h"
#include "../CommandIds.h"

CJournalTreeView::CJournalTreeView()
: CJournalView(new CTreeStyle){
	m_pViewJornal = NULL;
	m_viewFactory.Init(&m_state);
}
CJournalTreeView::~CJournalTreeView(){
	if(m_pViewJornal)
		m_pViewJornal->DestroyView();
}

void CJournalTreeView::CreateView(int hwnd){
	m_hWnd = (HWND)hwnd;
	CWnd* pWnd = CWnd::FromHandle((HWND)hwnd);
	m_tabCtrl.WindowCreate(pWnd);
	m_pViewJornal = m_viewFactory.GetViewJournal(State(JournalEnum::INSPECT_JOURNAL,
		JournalEnum::NOTSEL, JournalEnum::NONE));
	m_pViewJornal->CreateView();
	m_NavigationWin.WindowCreate(pWnd);
	CRect rect;
	GetClientRect(m_hWnd, rect);
	m_sizeWin = rect.Size();
}

BEGIN_MY_MESSAGE_MAP(CJournalTreeView)
	ON_WND_MSG(WM_SIZE, OnSize)
	ON_WND_MSG(WM_SETFOCUS, OnSetFocus)
	ON_WND_MSG(UM_ITEM_MANIPULATION, OnItemManipulation)
	ON_NOTIFY_MSG(TCN_SELCHANGE, JOURNAL_TAB_CTRL_ID, OnTabSelChange)
	ON_NOTIFY_MSG(TVN_SELCHANGED, TREE_NAVIGATION_WND_ID, OnTreeSelChange)
	ON_NOTIFY_MSG(TVN_ENDLABELEDIT, TREE_NAVIGATION_WND_ID, OnEndItemEdit)
END_MY_MESSAGE_MAP()

long CJournalTreeView::OnSize(unsigned int wParam, long lParam){
	m_sizeWin.SetSize(LOWORD(lParam), HIWORD(lParam) - SIZE_TOOLBAR);
	m_tabCtrl.MoveWindow(200, SIZE_TOOLBAR, LOWORD(lParam) - 200, 24);
	m_NavigationWin.MoveWindow(0, SIZE_TOOLBAR, 200, HIWORD(lParam) - SIZE_TOOLBAR);
	m_pViewJornal->Resize(200, 24 + SIZE_TOOLBAR, LOWORD(lParam) - 200, HIWORD(lParam) - 24 - SIZE_TOOLBAR);
	return 0;
}
long CJournalTreeView::OnSetFocus(unsigned int wParam, long lParam){
	m_pViewJornal->Focus();
	return 0;
}
long CJournalTreeView::OnTabSelChange(LPNMHDR pInfo){
	SelChange();
	return 0;
}
long CJournalTreeView::OnTreeSelChange(LPNMHDR pInfo){
	SelChange();
	return 0; 
}
long CJournalTreeView::OnEndItemEdit(LPNMHDR pInfo){
	LPNMTVDISPINFO info = (LPNMTVDISPINFO)pInfo;
	std::tstring strItemText = m_NavigationWin.GetItemText(info->item.hItem).GetString();
	if(!info->item.pszText || strItemText == info->item.pszText || !_tcslen(info->item.pszText))
		return 0;
	m_NavigationWin.SetItemText(info->item.hItem, info->item.pszText);
	IDatabase* pDatabase = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	long p = (long)m_NavigationWin.GetItemData(info->item.hItem);
	CRenameOperation operation;
	operation.m_content.s_type = (CRenameOperation::content::typeRename)LOWORD(p);
	operation.m_content.s_nId = HIWORD(p);
	operation.m_content.s_strNewName = info->item.pszText;
	operation.UpdateValue(pDatabase);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDatabase);
	return 0;
}
long CJournalTreeView::OnItemManipulation(unsigned int wParam, long lParam){
	ItemManipulation((JournalEnum::typeItemMnpl)wParam, lParam);
	return 0;
}

void CJournalTreeView::ItemManipulation(JournalEnum::typeItemMnpl type, int param){
	if(type == JournalEnum::DELETE_POINT ||
		type == JournalEnum::DELETE_FAMILY ||
		type == JournalEnum::DELETE_INSPECT){
			IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
			CDeleteOperation operation;
			operation.m_content.s_nId = param;
			if(type == JournalEnum::DELETE_POINT)
				operation.m_content.s_type = CDeleteOperation::content::POINT;
			else if(type == JournalEnum::DELETE_FAMILY)
				operation.m_content.s_type = CDeleteOperation::content::FAMILY;
			else if(type == JournalEnum::DELETE_INSPECT)
				operation.m_content.s_type = CDeleteOperation::content::JOURNAL;
			operation.DeleteValue(pDb);
			IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	} else {
		HTREEITEM item = m_NavigationWin.GetSelectedItem();
		long p = 0;
		if(item)
			p = (long)m_NavigationWin.GetItemData(item);
		if(type == JournalEnum::NONE)
			m_state.m_nIdParameter = HIWORD(p);
		else
			m_state.m_nIdParameter = param;
		ChangeState(State((JournalEnum::tabNumber)m_tabCtrl.GetCurSel(),
			(JournalEnum::typeTreeItem)LOWORD(p), type));
	}
}
void CJournalTreeView::SelChange(){
	HTREEITEM item = m_NavigationWin.GetSelectedItem();
	long p = 0;
	if(item)
		p = (long)m_NavigationWin.GetItemData(item);
	m_state.m_nIdParameter = HIWORD(p);
	if(m_state.m_nState > JournalEnum::VIEW_INSPECT)
		return;
	ChangeState(State((JournalEnum::tabNumber)m_tabCtrl.GetCurSel(),
		(JournalEnum::typeTreeItem)LOWORD(p), JournalEnum::NONE));
}
void CJournalTreeView::ChangeState(State state){
	m_pViewJornal->DestroyView();
	m_pViewJornal = m_viewFactory.GetViewJournal(state);
	m_pViewJornal->CreateView();
}
